package com.zzsy.carosshopprojects.base;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;

import com.igexin.sdk.PushManager;
import com.tencent.bugly.crashreport.CrashReport;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.service.CareoeIntentService;
import com.zzsy.carosshopprojects.service.CareoePushService;
import com.zzsy.carosshopprojects.util.sunmiprint.SunmiPrintHelper;
import com.zzsy.carosshopprojects.util.sunmireadcard.SunmiReadCardHelper;
import com.zzsy.carosshopprojects.util.zxing.DisplayUtil;

import androidx.multidex.MultiDex;
import me.yokeyword.fragmentation.BuildConfig;
import me.yokeyword.fragmentation.Fragmentation;

public class BaseApplation extends Application {
    public static BaseApplation instance() {
        return instance;
    }

    private static BaseApplation instance;

    @Override
    public void onCreate() {
        super.onCreate();
        initFragment();
        instance = this;
        // 屏幕适配
        ScreenAdapterTools.init(this);
        //初始化腾讯bugly
        CrashReport.initCrashReport(getApplicationContext(), "3aad456df5", false);
        //个推
        GeTuiData();
        //zxing
        initDisplayOpinion();
        //初始化商米打印机
        SunmiPrintHelper.getInstance().initSunmiPrinterService(this);
        //初始化商米支付服务为了可以能够识别卡片
        SunmiReadCardHelper.getInstance().connectPayService();

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        ScreenAdapterTools.getInstance().reset(this);
    }

    /**
     * 初始化Fragment管理工具
     */
    private void initFragment() {
        Fragmentation.builder()
                // 设置 栈视图 模式为 （默认）悬浮球模式   SHAKE: 摇一摇唤出  NONE：隐藏， 仅在Debug环境生效
                .stackViewMode(Fragmentation.BUBBLE)
                .debug(BuildConfig.DEBUG) // 实际场景建议.debug(BuildConfig.DEBUG)
                /**
                 * 可以获取到{@link me.yokeyword.fragmentation.exception.AfterSaveStateTransactionWarning}
                 * 在遇到After onSaveInstanceState时，不会抛出异常，会回调到下面的ExceptionHandler
                 */
                .handleException(e -> {
                    // 以Bugtags为例子: 把捕获到的 Exception 传到 Bugtags 后台。
                    // Bugtags.sendException(e);

                })
                .install();
    }

    private void GeTuiData() {
        PushManager.getInstance().initialize(instance, CareoePushService.class);
        PushManager.getInstance().registerPushIntentService(instance, CareoeIntentService.class);
    }

    private void initDisplayOpinion() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        DisplayUtil.density = dm.density;
        DisplayUtil.densityDPI = dm.densityDpi;
        DisplayUtil.screenWidthPx = dm.widthPixels;
        DisplayUtil.screenhightPx = dm.heightPixels;
        DisplayUtil.screenWidthDip = DisplayUtil.px2dip(getApplicationContext(), dm.widthPixels);
        DisplayUtil.screenHightDip = DisplayUtil.px2dip(getApplicationContext(), dm.heightPixels);
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


}
